import { ml_Toast } from '../../../utils/asyncWX'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address_data: [],
    ifadd: true, //判断是否是添加地址还是修改
    index: 0,
    picker: ['江西工业贸易职业技术学院'],
    address_inf: '',
    address_name: '',
    address_sex: true,
    address_phone: ''
  },
  switch(e) { this.setData({ address_sex: e.detail.value }); },
  address_inf(e) { this.setData({ address_inf: e.detail.value }); },
  address_name(e) { this.setData({ address_name: e.detail.value }); },
  address_phone(e) { this.setData({ address_phone: e.detail.value }); },

  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
  },
  showModal(e) {
    console.log(e.currentTarget.dataset);
    let items = e.currentTarget.dataset.item;
    if (items == 'add_address') {
      this.setData({
        modalName: e.currentTarget.dataset.target,
        ifadd: true,
      })
    } else {
      this.setData({
        modalName: e.currentTarget.dataset.target,
        ifadd: false,
        address_inf: items.address_inf,
        address_name: items.address_name,
        address_sex: items.address_sex,
        address_phone: items.address_phone
      })
    }

  },
  hideModal(e) {
    this.setData({
      modalName: null,
      index: 0,
      address_inf: '',
      address_name: '',
      address_sex: true,
      address_phone: ''
    })
  },
  ifnone() {
    if (this.data.address_inf == '') { return '请填写详细的门牌号！' }
    if (this.data.address_name == '') { return '请填写你的名字！' }
    if (this.data.address_phone == '') { return '请填写你的手机号！' }
    if (this.data.address_phone.length < 9 || this.data.address_phone.length > 12) { return '你的电话号码格式不符！' }
    return 200;
  },

  save_add(e) {
    let ifadd = this.data.ifadd;
    let address_data = wx.getStorageSync('address_data');
    if (ifadd) {
      //添加地址
      let returx = this.ifnone();
      if (returx == 200) {
        if (address_data == '') {
          wx.setStorageSync('address_data', [{
            address_inf: this.data.address_inf,
            address_name: this.data.address_name,
            address_sex: this.data.address_sex,
            address_phone: this.data.address_phone,
            address_school: this.data.picker[this.data.index]
          }])
        } else {
          address_data.push({
            address_inf: this.data.address_inf,
            address_name: this.data.address_name,
            address_sex: this.data.address_sex,
            address_phone: this.data.address_phone,
            address_school: this.data.picker[this.data.index]
          })
          wx.setStorageSync('address_data', address_data)
        }
      } else { ml_Toast(returx); }
    } else {
      //修改地址
      console.log(e)
    }
    console.log(ifadd)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ address_data: wx.getStorageSync('address_data') });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ address_data: wx.getStorageSync('address_data') });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})